package com.xuecheng.system.scanner;

import com.xuecheng.common.enums.StatusAnnotation;
import lombok.Data;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created on 2021/04/07/1:13 周三
 *
 * @author Lantin
 */
//@Component
public class MyScanner implements ApplicationListener<ContextRefreshedEvent> {

    public static List<Invoker> invokerList = new ArrayList<>();

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("*****************************应用监听事件开始了");
        if (event.getApplicationContext().getParent() == null) {
            Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(StatusAnnotation.class);

            for (Object bean : beans.values()) {
                StatusAnnotation ca = bean.getClass().getAnnotation(StatusAnnotation.class);
                System.out.println(bean.getClass().getName()+"==="+ca.name()+"==="+ca.code());

            }
        }


    }

    @Data
    class Invoker {
        private Method method;
        private String tag;
        private Object bean;

        public Invoker(Method method, String tag, Object bean) {
            this.method = method;
            this.tag = tag;
            this.bean = bean;
        }
    }

}
